package controllers
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import models.DebuggerHistoryEntry;
	import models.Variable;
	
	import mx.core.Application;
	import mx.rpc.events.ResultEvent;
	
	import ui.notifications.AlertAskNotification;
	import ui.windows.AddWatchWindow;
	import ui.windows.DebugAddWatchWindow;
	
	public class WatchController extends Object
	{
		public function WatchController()
		{
			super();
		}
		
		public function showAddWatchWindow():void{
			var win:AddWatchWindow = new AddWatchWindow();
			win.popUp();
		}
		
		public function addWatch(expression:String):void{
			// Ignore if the expression is already being watched
			if(expression in Application.application.watches.watchList)
				return;
			
			var watch:Variable = new Variable();
			watch.name = expression;
			watch.scope = "watch";
			
			Application.application.watches.watchList.addItem(watch);
			
			// Find out the current value of the watch variable
			var variable:Variable = Application.application.pyStates.currentState.findVariableByName(expression);
			if(variable){
				watch.value = variable.value;
			}
		}
		
		public function removeWatch(watch:Variable):void{
			var index:Number = Application.application.watches.watchList.getItemIndex(watch);
			Application.application.watches.watchList.removeItemAt(index);
		}
		
		public function showRemoveAllWarning():void{
			if(Application.application.watches.watchList.length == 0)
				return;
				
			var alert:AlertAskNotification = new AlertAskNotification();
			alert.text = "Remove all the watch variables?";
			alert.popUp();
			
			alert.confirmButton.addEventListener(MouseEvent.CLICK, removeAll);
		}
		
		public function removeAll(evt:Event = null):void{
			Application.application.watches.watchList.removeAll();
		}
		
		// Refresh the debugger watches (show all watch values for the current debugger session)
		public function updateDebuggerWatches(entry:DebuggerHistoryEntry):void{
			for each(var variable:Variable in entry.variables){
				var watch:Variable = Application.application.debuggerWatches.findWatchByName(variable.name);
				if(watch)
					watch.value = variable.value;
			}
		}
		
		// Add a watch for debugger
		public function debuggerShowAddWatchWindow():void{
			var win:DebugAddWatchWindow = new DebugAddWatchWindow();
			win.popUp();
		}
		
		public function debuggerAddWatch(expression:String):void{
			// Ignore if the expression is already being watched
			if(expression in Application.application.debuggerWatches.watchList)
				return;
			
			Application.application.debuggerController.send("watch " + expression);
			
			var watch:Variable = new Variable();
			watch.name = expression;
			watch.scope = "watch";
			
			Application.application.debuggerWatches.watchList.addItem(watch);
		}
		
		// Remove a watch for debugger
		public function debuggerRemoveWatch(watch:Variable):void{
			Application.application.debuggerController.send("remove_watch " + watch.name);
			var index:Number = Application.application.debuggerWatches.watchList.getItemIndex(watch);
			Application.application.debuggerWatches.watchList.removeItemAt(index);
		}
		
		// Remove all watch
		public function debuggerShowRemoveAllWarning():void{
			if(Application.application.debuggerWatches.watchList.length == 0)
				return;
			
			var alert:AlertAskNotification = new AlertAskNotification();
			alert.text = "Remove all the watch variables?";
			alert.popUp();
			
			alert.confirmButton.addEventListener(MouseEvent.CLICK, debuggerRemoveAll);
		}

		public function debuggerRemoveAll(evt:Event = null):void{
			Application.application.debuggerController.send("remove_all_watch");
			Application.application.debuggerWatches.watchList.removeAll();
		}
		
	}
}